#include <stdio.h>
#include <mxml.h>
#include <assert.h>

void test00(int argc, char* argv[])
{
         FILE *fp  = NULL;

         int k = 0;

         mxml_node_t * tree = NULL;
         mxml_node_t * node  = NULL;

         if (argc < 2){
             perror("Argument Required XML File ");
             exit(1);
         }
         fp = fopen (argv[1], "r");
         if (fp ){
             tree = mxmlLoadFile (NULL , fp , MXML_OPAQUE_CALLBACK);
         }else {
             perror("Could Not Open the File Provided");
             exit(1);
         }
         if (tree){
                 for (node = mxmlFindElement(tree, tree,NULL,NULL, NULL,MXML_DESCEND);
                         node != NULL;
                         node=mxmlWalkNext (node, NULL, MXML_DESCEND)
                         //node = mxmlFindElement(node, tree, NULL,NULL,NULL,MXML_DESCEND)
                 ){
                         if (node->type  == MXML_ELEMENT) {
                             printf("MXML_ELEMENT Node <%s>:%d \n", node->value.element.name, node->value.element.num_attrs);
                             for (k = 0; k < node->value.element.num_attrs; k++){
                                 if (node->value.element.attrs ){
                                     printf ("Attribute Name :: %s \n", node->value.element.attrs[k].name);
                                     printf ("Attribute Value:: %s \n", node->value.element.attrs[k].value);
                                 }
                                 //if (!strncmp(node->value.element.name , "display-name", 12 )){
                                 //    printf(" String %s \n", (char*) node->child->value.text.string);
                                 //}
                             }
                         }
                         else if (node->type == MXML_REAL){
                             printf("MXML_REAL Node is %s \n", node->value.element.name);
                         }
                         else if(node->type == MXML_OPAQUE){
                             printf("MXML_OPAQUE Node is %s \n", node->value.element.name);
                         }
                         else if(node->type == MXML_INTEGER){
                             printf("MXML_INTEGER Node is %s \n", node->value.element.name);
                         }
                         else if(node->type == MXML_TEXT){
                             printf("MXML_TEXT Node is %s \n", node->value.element.name);
                         }
                         else if(node->type == MXML_IGNORE){
                             printf("MXML_IGNORE Node is %s \n", node->value.element.name);
                         }
                         else if(node->type == MXML_CUSTOM){
                             printf("MXML_IGNORE Node is %s \n", node->value.element.name);
                         }
                         else {
                             printf("Type Default Node is %s \n", node->value.element.name);
                         }
                 }
         }
         if (tree){
            mxmlDelete(tree);
         }
         if (fp){
            fclose(fp);
         }
}

void test01(int argc, char* argv[])
{
  mxml_node_t *xml;
  mxml_node_t *data;
  xml = mxmlNewXML("1.0");
  assert(argc>=1);
  char* fileName=argv[1];
 
  data = mxmlNewElement(xml, "settings");
 
  char set1[3];
  sprintf(set1, "%d", 1);
  mxmlElementSetAttr(data, "setting_background_music", set1);
  char set2[3];
  sprintf(set2, "%d", 2);
  mxmlElementSetAttr(data, "setting_rumble", set2);
  char set3[3];
  sprintf(set3, "%d", 3);
  mxmlElementSetAttr(data, "setting_tips", set3);
 
#if 01
{
  const char* FieldName="setting_background_music";
  const char* FieldVal=mxmlElementGetAttr(data, FieldName);
  assert(FieldVal);
  printf("(%s:%d) '%s'='%s'\n",__FILE__,__LINE__,FieldName, FieldVal);
}
{
  const char* FieldName="setting_rumble";
  const char* FieldVal=mxmlElementGetAttr(data, FieldName);
  assert(FieldVal);
  printf("(%s:%d) '%s'='%s'\n",__FILE__,__LINE__,FieldName, FieldVal);
}
{
  const char* FieldName="setting_tips";
  const char* FieldVal=mxmlElementGetAttr(data, FieldName);
  assert(FieldVal);
  printf("(%s:%d) '%s'='%s'\n",__FILE__,__LINE__,FieldName, FieldVal);
}
#endif
  FILE *f = fopen(fileName, "wb");
 
  if (f == NULL) {
    fclose(f);
    printf("Settings could not be written.\n");
  }
  else {
    assert(xml);
    assert(f);
    mxmlSaveFile(xml, f, MXML_NO_CALLBACK);
    fclose(f);
    mxmlDelete(data);
    mxmlDelete(xml);
    printf("Settings Saved\n");
  }
}

int main(int argc, char* argv[])
{
  printf("(%s:%d) main process initializing\n",__FILE__,__LINE__);
  assert(argc>1);
  test01(argc,argv);
  test00(argc,argv);
  printf("(%s:%d) main process terminating\n",__FILE__,__LINE__);
}
